const ProjectDetailService = require("../srevice/projectDetailService");

const projectDetailService = new ProjectDetailService();
const { projectDetailsGetError, deleteProjectError } = require("../main/err.type")

class projectDetailController {
    // 创建项目
    async createProject(ctx) {
        try {
            // 主项目id，项目名称，项目周期，负责人
            const {
                projectProgress,
                projectName,
                projectLeader,
                deliveryDate,
                status,
                projectPersonnel
            } = ctx.request.body;
            console.log(projectProgress,
                projectName,
                projectLeader,
                deliveryDate,
                status,
                projectPersonnel);
            const res = await projectDetailService.createProject({
                projectProgress,
                projectName,
                projectLeader,
                deliveryDate,
                status,
                projectPersonnel
            });
            console.log(res);
            let sendMsg = {
                code: 200,
                data: '创建成功'
            }
            ctx.body = sendMsg
        } catch (error) {
            console.log(error);
            ctx.body = projectDetailsBranchCreateError;
            return ctx.app.emit("error", projectDetailsBranchCreateError, ctx);
        }
    }
    // 分页 获取项目
    async getProjectByPage(ctx) {
        try {
            const {
                pageSize,
                currentPage
            } = ctx.query;
            const res = await projectDetailService.getProjectByPage({
                pageSize,
                currentPage
            });
            let sendMsg = {
                code: 200,
                data: res
            }
            ctx.body = sendMsg
        } catch (error) {
            console.log(error);
            ctx.body = projectDetailsGetError;
            return ctx.app.emit("error", projectDetailsGetError, ctx);
        }
    }
    // 获取项目
    async getProject(ctx) {
        try {
            const {
                id
            } = ctx.query;
            const res = await projectDetailService.getProject({
                id
            });
            let sendMsg = {
                code: 200,
                data: res
            }
            ctx.body = sendMsg
        } catch (error) {
            console.log(error);
            ctx.body = projectDetailsGetError;
            return ctx.app.emit("error", projectDetailsGetError, ctx);
        }
    }
    // 删除项目 将isDelte字段置为true
    async deleteProject(ctx) {
        try {
            const {
                id
            } = ctx.request.body;
            const res = await projectDetailService.deleteProject({
                id
            });
            if (res == 0) {
                let sendMsg = {
                    code: 200,
                    data: '删除失败'
                }
                ctx.body = sendMsg
            } else {
                let sendMsg = {
                    code: 200,
                    data: '删除成功'
                }
                ctx.body = sendMsg
            }
            console.log(res == 0);
        } catch (error) {
            console.log(error);
            ctx.body = deleteProjectError;
            return ctx.app.emit("error", deleteProjectError, ctx);
        }
    }
    // 编辑项目
    async editProject(ctx) {
        try {
            const {
                id,
                projectName,
                projectLeader
            } = ctx.request.body;
            const res = await projectDetailService.editProject({
                id,
                projectName,
                projectLeader
            });
            let sendMsg = {
                code: 200,
                data: '编辑成功'
            }
            console.log(res);
            ctx.body = sendMsg
        } catch (error) {
            console.log(error);
            ctx.body = projectDetailsBranchEditError;
            return ctx.app.emit("error", projectDetailsBranchEditError, ctx);
        }
    }
    // 通过年月日 id 分页查询 
    async getProjectByDate(ctx) {
        try {
            const {
                pageSize,
                currentPage,
                year,
                month,
                week
            } = ctx.query;
            const res = await projectDetailService.getProjectByDate({
                pageSize,
                currentPage,
                year,
                month,
                week
            });
            let sendMsg = {
                code: 200,
                data: res
            }
            ctx.body = sendMsg
        } catch (error) {
            console.log(error);
            ctx.body = projectDetailsGetError;
            return ctx.app.emit("error", projectDetailsGetError, ctx);
        }
    }
}


module.exports = new projectDetailController(); 